#include "gamewindow.h"
#include "./ui_widget.h"


GameWindow::GameWindow(QWidget *parent): QWidget{parent}
    , ui(new Ui::Widget)
    //播放器设置
    , player(new QMediaPlayer(this)), audioOutPut(new QAudioOutput(this))
{ // 设置窗口标题和大小
    setWindowTitle("Game Window1");
    resize(800, 600);  // 设定窗口大小

    ui->setupUi(this);
    // 关联播放器和音频输出设备
    player->setAudioOutput(audioOutPut);
    // 设置音量（0.0 ~ 1.0）
    audioOutPut->setVolume(0.5);
    // 播放背景音乐
    playBackGroundMusic("C:/Users/Annie Tang/Music/Laufey - Letter To My 13 Year Old Self.flac");

    // 创建背景标签并设置图片
    backgroundLabel = new QLabel(this);
    backgroundLabel->setPixmap(QPixmap("D:/QtCreator/HeartIslandTest/figures/rainypic1.jpg"));
    backgroundLabel->setScaledContents(true);
    backgroundLabel->resize(this->size());
    backgroundLabel->move(0, 0);  // 让背景从窗口的左上角开始显示

    // 将背景标签置于其他控件之下
    backgroundLabel->raise();  // 使得背景层始终位于底部

    puppyMessageLabel=new QLabel(this);

    rainTimer = new QTimer(this);
    connect(rainTimer, &QTimer::timeout, this, &GameWindow::createRaindrop);
    rainTimer->start(800);  // 每 800ms 生成一个雨滴

    puppy = new QLabel(this);
    puppy->setScaledContents(true);
    puppy->resize(200, 200);  // 设定小狗的大小

    // 让小狗出现在窗口底部中央
    int xPos = width() / 2 - puppy->width() / 2;
    int yPos = height() - 200;
    puppy->move(xPos, yPos);

    // 让小狗播放动画（GIF）
    puppyMovie = new QMovie("D:/QtCreator/HeartIslandTest/figures/Dogs Love Sticker by Chippy the Dog.gif");
    puppy->setMovie(puppyMovie);
    puppyMovie->start();  // 开始播放 GIF

    puppy->show();


}

void GameWindow::createRaindrop() {
    QLabel *raindrop = new QLabel(this);
    raindrop->setPixmap(QPixmap("D:/QtCreator/HeartIslandTest/figures/raindrop.gif"));
    raindrop->setScaledContents(true);
    raindrop->resize(55, 40);

    int x = QRandomGenerator::global()->bounded(width());  // 随机 X 坐标
    raindrop->move(x, 0);
    raindrop->show();
    raindrops.append(raindrop);

    // 让雨滴下落动画
    QPropertyAnimation *fallAnimation = new QPropertyAnimation(raindrop, "pos");
    fallAnimation->setDuration(3000);
    fallAnimation->setStartValue(QPoint(x, 0));
    fallAnimation->setEndValue(QPoint(x, height()));
    fallAnimation->setEasingCurve(QEasingCurve::Linear);
    fallAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void GameWindow::mousePressEvent(QMouseEvent *event) {
    for (QLabel *raindrop : raindrops) {
        if (raindrop->geometry().contains(event->pos())) {
            fadeRaindrop(raindrop);  // 让雨滴变成星星
            clickCount++;

            if (clickCount % 5 == 0) {
                showPuppyMessage();  // 让小狗说话
            }
            if (clickCount == 20) {
               showPuppyMessage();
            }
            break;
        }
    }
}

void GameWindow::fadeRaindrop(QLabel *raindrop) {
    QMovie *movie = new QMovie("D:/QtCreator/HeartIslandTest/figures/New York Yes Sticker by Nora Fikse.gif");
    raindrop->setMovie(movie);
    raindrop->setScaledContents(true);
    movie->start();

    // 确保 GIF 播放完后才删除
    connect(movie, &QMovie::finished, this, [=]() {
        movie->deleteLater();  // 先删除 QMovie
        QTimer::singleShot(100, raindrop, &QLabel::deleteLater);  // 再删除 QLabel
    });
}

void GameWindow::setBackground(const QString &backgroundPath) {
    QPixmap pixmap(backgroundPath);
    backgroundLabel->setPixmap(pixmap.scaled(backgroundLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}


void GameWindow::showPuppyMessage() {
    static QList<QPair<QString, QPair<QString, QString>>> messages = {
        QPair<QString, QPair<QString, QString>>(
            "没关系的，用拥抱带走难过吧",
            QPair<QString, QString>("D:/QtCreator/HeartIslandTest/figures/huggypuppy.gif", "D:/QtCreator/HeartIslandTest/figures/rainypic2.jpg")
            ),
        QPair<QString, QPair<QString, QString>>(
            "辛苦啦，躺下吧，我帮你赶走所有坏梦！",
            QPair<QString, QString>("D:/QtCreator/HeartIslandTest/figures/sleepypuppy.gif", "D:/QtCreator/HeartIslandTest/figures/rainypic3.jpg")
            ),
        QPair<QString, QPair<QString, QString>>(
            "要用尾巴赶走烦恼？我帮你摇！",
            QPair<QString, QString>("D:/QtCreator/HeartIslandTest/figures/iloveyoupuppy.gif", "D:/QtCreator/HeartIslandTest/figures/rainypic4.jpg")
            ),
        QPair<QString, QPair<QString, QString>>(
            "不管什么时候，你都不是一个人。",
            QPair<QString, QString>("D:/QtCreator/HeartIslandTest/figures/I Love You Ily Sticker by Chippy the Dog.gif", "D:/QtCreator/HeartIslandTest/figures/rainypic5.jpg")
            ),
        QPair<QString, QPair<QString, QString>>(
            "这场雨会停的，和我一起等等吧？",
            QPair<QString, QString>("D:/QtCreator/HeartIslandTest/figures/waitingpuppy.gif", "D:/QtCreator/HeartIslandTest/figures/rainypic6.jpg")
            )
    };

    // 随机选择一条消息
    int index = QRandomGenerator::global()->bounded(messages.size());

    // 获取文本、GIF路径和背景路径
    QString messageText = messages[index].first;
    QString gifPath = messages[index].second.first;        // 获取GIF路径
    QString backgroundPath = messages[index].second.second; // 获取背景路径

    // 更新背景
    setBackground(backgroundPath);


    // 确保 puppyMovie 存在
    if (puppyMovie) {
        puppyMovie->stop();
        puppyMovie->deleteLater();
    }
    puppyMovie = new QMovie(gifPath);
    puppy->setMovie(puppyMovie);
    puppyMovie->start();

    // 确保不重复创建 QLabel
    if (puppyMessageLabel) {
        puppyMessageLabel->deleteLater();
    }
    puppyMessageLabel = new QLabel(messageText, this);
    puppyMessageLabel->setStyleSheet("font-size: 20px; color: pink; background: rgba(0, 0, 0, 0.5); border-radius: 10px;");
    puppyMessageLabel->move(puppy->x(), puppy->y() - 30);
    puppyMessageLabel->show();

    // 2秒后删除
    QTimer::singleShot(4000, this, [=]() {
        if (puppyMessageLabel) {
            puppyMessageLabel->hide();

        }
    });
}


void GameWindow::playBackGroundMusic(const QString &filePath) {
    player->setSource(QUrl::fromLocalFile(filePath));  // 设置音乐文件
    player->play();  // 开始播放
}
void GameWindow::closeEvent(QCloseEvent *event) {
    emit gameWindowClosed(); // 非常关键
    QWidget::closeEvent(event);
}




GameWindow::~GameWindow(){
    delete ui;
};

